package com.tools;

import java.io.InputStream;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import com.dailyyoga.engine.R;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.PorterDuff.Mode;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.util.Log;

@SuppressLint("SimpleDateFormat")
public class DailyYogaTools {

	public static final String downloadURL = "https://market.android.com/details?id=";

	public static String getFormatDate(int year, int month, int day) {

		String _month = "";

		if (month > 9) {
			_month = ("" + month);
		} else {
			_month = ("0" + month);
		}

		String _day = "";
		if (day > 9) {
			_day = ("" + day);
		} else {
			_day = ("0" + day);
		}

		return year + "-" + _month + "-" + _day;

	}

	public static Date getFormatTime(String formatDate) throws ParseException {

		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");

		return dateFormat.parse(formatDate);

	}

	public static String getSystemCurrentTime() {

		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
		return dateFormat.format(new Date());
	}

	public static long format2millis(String formatTime) throws ParseException {

		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");

		return dateFormat.parse(formatTime).getTime();
	}

	public static Bitmap toRoundCorner(Bitmap bitmap) {
		int w = bitmap.getWidth() > bitmap.getHeight() ? bitmap.getWidth()
				: bitmap.getHeight();

		Bitmap output = Bitmap.createBitmap(w, w, Config.ARGB_8888);
		Canvas canvas = new Canvas(output);

		final int color = 0xffffffff;

		final Rect src = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());

		int X = (w - bitmap.getWidth()) / 2;
		int Y = (w - bitmap.getHeight()) / 2;

		final Rect dst = new Rect(X, Y, bitmap.getWidth() + X,
				bitmap.getHeight() + Y);

		Paint paint = new Paint();
		paint.setAntiAlias(true);
		canvas.drawARGB(0, 0xff, 0xff, 0xff);
		paint.setColor(color);
		int x = w;
		canvas.drawCircle(x / 2, x / 2, x / 2, paint);
		paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
		canvas.drawBitmap(bitmap, src, dst, paint);
		canvas.save();
		bitmap.recycle();

		return output;
	}

	public static String formatCountdown(String formatTime, Context context)
			throws ParseException {
		SimpleDateFormat dateFormat = new SimpleDateFormat(
				"yyyy-MM-dd HH:mm:ss");
		long time = System.currentTimeMillis()
				- dateFormat.parse(formatTime).getTime();

		Log.d("countdown",
				formatTime
						+ "="
						+ new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
								.format(System.currentTimeMillis()) + "=="
						+ time);

		if (time / (60 * 1000) < 1) {
			String countdown = context.getString(R.string.time_0);
			Log.d("countdown", countdown);
			return countdown;
		}

		if (time / (60 * 1000) < 60) {
			String countdown = context.getString(R.string.time_1).replace("%",
					"" + time / (60 * 1000));
			Log.d("countdown", countdown);

			return countdown;
		}

		if (time / (60 * 60 * 1000) < 24) {

			String countdown = context.getString(R.string.time_2).replace("%",
					"" + time / (60 * 60 * 1000));
			Log.d("countdown", countdown);

			return countdown;

		}

		if ((time / (24 * 60 * 60 * 1000)) < 7) {

			String countdown = context.getString(R.string.time_3).replace("%",
					"" + time / (24 * 60 * 60 * 1000));
			Log.d("countdown", countdown);

			return countdown;
		}

		if ((time / (24 * 60 * 60 * 1000)) < 360) {

			String countdown = new SimpleDateFormat("M-dd HH:mm")
					.format(dateFormat.parse(formatTime).getTime());
			Log.d("countdown", countdown);
			return countdown;
		}

		return formatTime;

	}

	public static String millis2format(long millis) {

		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");

		return dateFormat.format(new Date(millis));
	}

	@SuppressWarnings("deprecation")
	public static String formatDateAddformatDate(String formatDate1,
			String formatHm) throws ParseException {

		Log.d("formatDateAddformatDate", "formatDate1=" + formatDate1);
		Log.d("formatDateAddformatDate", "formatHm=" + formatHm);

		Calendar calendar = Calendar.getInstance();

		Date date1 = getFormatTime(formatDate1);

		calendar.setTimeInMillis(date1.getTime());

		Date date2 = getFormatTime("2013-12-02 " + formatHm);

		calendar.add(Calendar.HOUR, date2.getHours());

		calendar.add(Calendar.MINUTE, date2.getMinutes());

		String result = new SimpleDateFormat("yyyy-MM-dd HH:mm")
				.format(calendar.getTime());
		Log.d("formatDateAddformatDate", "result=" + result);

		return result;

	}

	@SuppressWarnings("deprecation")
	public static int[] date2HM(String date) throws ParseException {

		int[] hm = new int[2];

		Date date2 = getFormatTime(date);

		hm[0] = date2.getHours();
		hm[1] = date2.getMinutes();

		return hm;

	}

	public static boolean pkgIsInstall(String inPkg, Context inContext) {
		try {

			inContext.createPackageContext(inPkg,
					Context.CONTEXT_IGNORE_SECURITY
							| Context.CONTEXT_INCLUDE_CODE);
		} catch (NameNotFoundException e) {
			return false;
		}

		return true;
	}

	public static Context getOtherContext(Context inContext, String inPkg) {

		try {
			return inContext.createPackageContext(inPkg,
					Context.CONTEXT_IGNORE_SECURITY
							| Context.CONTEXT_INCLUDE_CODE);
		} catch (NameNotFoundException e) {
			return null;
		}

	}

	public static Bitmap getPlugPrompt(Context inContext, String inPkg,
			String inPrompt) {

		try {
			Context plugContext = getOtherContext(inContext, inPkg);
			InputStream inputStream = plugContext.getAssets().open(inPrompt);
			Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
			inputStream.close();
			return bitmap;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}

	}

	public static int getPgkVc(Context inContext, String inPkgName) {

		int ic = -1;
		PackageManager packageManager = inContext.getPackageManager();
		PackageInfo pi = null;
		try {
			pi = packageManager.getPackageInfo(inPkgName, 0);
			ic = pi.versionCode;
		} catch (NameNotFoundException e) {
			// TODO Auto-generated catch block
			ic = -1;
		}
		return ic;

	}

	public static String fixUserName(String firstName, String lastName) {

		if (firstName.contains("腾讯QQ") || firstName.contains("新浪微博")) {
			firstName = "";
		}

		if (lastName.contains("腾讯QQ") || lastName.contains("新浪微博")) {
			lastName = "";
		}

		return firstName + " " + lastName;
	}

}
